import { getLocale } from 'umi';
import { isObject, merge, isString } from 'lodash';
import { DynamicObjectAny } from '@/typings';
export const wrapperRaf = (callback: () => void): number => {
  return typeof window !== 'undefined' && 'requestAnimationFrame' in window
    ? window.requestAnimationFrame(callback)
    : +setTimeout(callback, 16);
};

wrapperRaf.cancel =
  typeof window !== 'undefined' && 'requestAnimationFrame' in window
    ? (handle: number) => window.cancelAnimationFrame(handle)
    : (num: number) => clearTimeout(num);

export const getClientSize = (): { width: number; height: number } => {
  const { clientHeight, clientWidth } = document.documentElement;
  return {
    width: clientWidth,
    height: window.innerHeight || clientHeight,
  };
};

const fixPoint = (key: 'x' | 'y', start: number, width: number, clientWidth: number) => {
  const startAddWidth = start + width;
  const offsetStart = (width - clientWidth) / 2;

  if (width > clientWidth) {
    if (start > 0) {
      return {
        [key]: offsetStart,
      };
    }
    if (start < 0 && startAddWidth < clientWidth) {
      return {
        [key]: -offsetStart,
      };
    }
  } else if (start < 0 || startAddWidth > clientWidth) {
    return {
      [key]: start < 0 ? offsetStart : -offsetStart,
    };
  }
  return {};
};

export const getFixScaleEleTransPosition = (
  width: number,
  height: number,
  left: number,
  top: number,
): null | { x?: number; y?: number } => {
  const { width: clientWidth, height: clientHeight } = getClientSize();

  let fixPos = null;

  if (width <= clientWidth && height <= clientHeight) {
    fixPos = {
      x: 0,
      y: 0,
    };
  } else if (width > clientWidth || height > clientHeight) {
    fixPos = {
      ...fixPoint('x', left, width, clientWidth),
      ...fixPoint('y', top, height, clientHeight),
    };
  }

  return fixPos;
};

export function toArray<T>(arr: T | readonly T[]): T[] {
  if (arr === undefined || arr === null) {
    return [];
  }
  return (Array.isArray(arr) ? arr : [arr]) as T[];
}

export const arrayToNestJson = (arr: string[], value: any) => {
  const { length } = arr;
  let temp = {};
  let result = {};
  const lastIndex = length - 1;
  let index = lastIndex;
  while (index >= 0) {
    const key = arr[index];
    result[key] = index === lastIndex ? value : temp;
    if (index > 0) {
      temp = result;
      result = {};
    }
    index = index - 1;
  }
  return result;
};

export const getLocal = () => {
  const locale = getLocale();
  return (locale && locale.replace('-', '_')) || 'zh_CN';
};
export const transformValue = (record: any, changeKey?: string) => {
  return isObject(record) && changeKey ? record[changeKey] : record;
};

export const isValueEqual = (
  val: DynamicObjectAny | string,
  newVal: DynamicObjectAny | string,
): boolean => {
  if (isString(val) && isString(newVal)) {
    return val === newVal;
  }

  if (isObject(val) && isObject(newVal)) {
    return val.value === newVal.value;
  }

  if (isString(val) && isObject(newVal)) {
    return val === newVal.value;
  }

  if (isObject(val) && isString(newVal)) {
    return val.value === newVal;
  }

  return false;
};

const HEX = 'zyxwvutsrqponmlkjihgfedcba9876543210';

export const uid = (len?: number) => {
  let str = '';
  let num = len || 6;

  while (num--) {
    str += HEX[(Math.random() * 36) | 0];
  }
  return str;
};
